'''
Created on 06/06/2013

@author: rhernandezm
'''
import admin.jms.resources.AdminJMSResources as adminJMS
import admin.sib.AdminBuses as adminBuses
import admin.sib.resources.AdminSIBResources as adminSIBRes

nodes = [
             ['CRI400WP_Node2',
              [ 
               ['BECV1Server641', 'BECV1Server642'],
               ['SEC3Server641', 'SEC3Server642'],
               ['SEC3ProcessServer641', 'SEC3ProcessServer642'],
               ['MADServer641', 'MADServer642']
              ]
             ]
            ]

cvServers = [['BECV1Server641', 'CRI400WP_Node2', 'apsCVQueue1', 'apsInputQueue', 'jms/apsInputQueue'], ['BECV1Server642', 'CRI400WP_Node2', 'apsCVQueue2']]

psDestinationsByServer = [['SEC3ProcessServer641', 'apsQueue1', 'CRI400WP_Node2', 'apsInputQueue', 'jms/apsInputQueue'], ['SEC3ProcessServer642', 'apsQueue2', 'CRI400WP_Node2']]

busName = 'DefaultSBESIB'
#BusMembersCR
#busMembers = ['SEC3Server641', 'SEC3Server642', 'SEC3Server643', 'SEC3Server644','SEC3Server645', 'SEC3Server646', 'SEC3Server647', 'SEC3Server648', 'SEC3Server649', 'SEC3Server6410',  'SEC3ServerAS1', 'SEC3ServerAS2', 'SEC3ServerAS3', 'SEC3ServerAS4', 'SEC3ServerAS5', 'SEC3ServerAS6', 'SEC3ServerAS7', 'SEC3ServerAS8', 'SEC3ServerAS9', 'SEC3ServerAS10']

print 

for node in nodes:
    adminJMS.removeMQQueue('/Node:' + node[0], 'apsInputQueue')
    adminJMS.removeMQQueueConnectionFactory('/Node:' + node[0], 'apsQCF')
    

for cvServer in cvServers:
    adminJMS.removeMQQueue('/Server:' + cvServer[0], 'apsInputQueue')
    adminJMS.removeMQQueueConnectionFactory('/Server:' + cvServer[0], 'apsQCF')

#Create Bus
adminBuses.createBus(busName)

#Add members to the bus
for node in nodes:
    for serverGroup in node[1]:
        for server in serverGroup:
            print 'Adding: ' + server
            adminBuses.addServerAsBusMember(busName, node[0] , server)
            

for destination in psDestinationsByServer:
    print destination
    adminBuses.createBusDestinationForServer(busName, destination[1], destination[0], destination[2])
    if len(destination) > 3:
        adminSIBRes.createSIBQueue('/Node:' + destination[2], destination[3], destination[4], busName, destination[1])

for cvDestination in cvServers:
    print cvDestination
    adminBuses.createBusDestinationForServer(busName, cvDestination[2], cvDestination[0], cvDestination[1])
    if len(cvDestination) > 3:
        adminSIBRes.createSIBQueue('/Server:' + cvDestination[0], cvDestination[3], cvDestination[4], busName, cvDestination[2])

for node in nodes:
    adminSIBRes.createSIBQueueConnectionFactory('/Node:' + node[0], 'apsQCF', 'jms/apsQCF', busName, [['maxConnections', '50'], ['connectionTimeout', '10']])

for cvDestination in cvServers:
    adminSIBRes.createSIBQueueConnectionFactory('/Server:' + cvDestination[0], 'apsQCF', 'jms/apsQCF', busName, [['maxConnections', '50'], ['connectionTimeout', '10']])
    

######### Saving configuration ############
print "     Saving configuration."
AdminConfig.save()
print "     Done."